package com.mcy.springbatch.job;

import com.mcy.springbatch.entity.SysUser;
import com.mcy.springbatch.job.BaseJobConfig;
import com.mcy.springbatch.processer.SysUserRetryProcessor;
import com.mcy.springbatch.writer.SysUserItemWriter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.item.ItemReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * job错误重试的Demo
 *
 * @author : Create By mcy
 * @date : 2019/8/25 17:46:58
 */

@Slf4j
@Configuration
public class NRetryJobJobConfig extends BaseJobConfig {

    @Autowired
    ItemReader userDataListItemReader;

    @Autowired
    private SysUserRetryProcessor sysUserRetryProcessor ;

    @Autowired
    SysUserItemWriter sysUserItemWriter;

    @Bean
    public Job retryJob() {
        return jobBuilderFactory.get("retryJob")
                .incrementer(new RunIdIncrementer())
                .start(retryJobStep())
                .build();
    }

    @Bean
    public Step retryJobStep() {
        return stepBuilderFactory.get("retryJobStep")
                .<SysUser, SysUser>chunk(10)
                .reader(userDataListItemReader)
                .processor(sysUserRetryProcessor)
                .writer(sysUserItemWriter)
                .faultTolerant()
                .retry(Exception.class)//可指定具体异常
                .retryLimit(10)
                .build();
    }
}

